<html>
<head><meta charset="utf-8"><title>struct literals in match · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/struct.20literals.20in.20match.html">struct literals in match</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="227375768"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/struct%20literals%20in%20match/near/227375768" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/struct.20literals.20in.20match.html#227375768">(Feb 23 2021 at 02:55)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">Foo</span><span class="w"> </span><span class="p">{</span><span class="n">foo</span>: <span class="kt">bool</span><span class="p">}</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="bp">self</span><span class="p">.</span><span class="n">foo</span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="n">foo</span>: <span class="nc">true</span><span class="p">}.</span><span class="n">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<div class="codehilite"><pre><span></span><code>error: struct literals are not allowed here
 --&gt; src/main.rs:7:11
  |
7 |     match Foo {foo: true}.foo() {
  |           ^^^^^^^^^^^^^^^
  |
help: surround the struct literal with parentheses
  |
7 |     match (Foo {foo: true}).foo() {
  |           ^               ^
</code></pre></div>
<p>Usually when rust has weird edge case syntax restrictions there is some reason for it such as defense against future syntax extensions. What's the reason for this one?</p>



<a name="227375846"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/struct%20literals%20in%20match/near/227375846" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/struct.20literals.20in.20match.html#227375846">(Feb 23 2021 at 02:56)</a>:</h4>
<p>there is a similar restriction in <code>if</code> conditions as well</p>



<a name="227377074"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/struct%20literals%20in%20match/near/227377074" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/struct.20literals.20in.20match.html#227377074">(Feb 23 2021 at 03:15)</a>:</h4>
<p><span class="user-mention" data-user-id="271719">@Mario Carneiro</span> <a href="https://github.com/rust-lang/rfcs/blob/master/text/0092-struct-grammar.md">https://github.com/rust-lang/rfcs/blob/master/text/0092-struct-grammar.md</a></p>



<a name="227380156"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/struct%20literals%20in%20match/near/227380156" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/struct.20literals.20in.20match.html#227380156">(Feb 23 2021 at 04:11)</a>:</h4>
<p>Oh, I hadn't even noticed the ambiguity. Here's a fun version:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">Foo</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="k">enum</span> <span class="nc">Void</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">bar</span>: <span class="nc">Void</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{}</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="n">bar</span><span class="w"> </span><span class="p">{}</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>The first is an incomplete match on a unit struct (or would be if not for the RFC), and the second is an empty match on an uninhabited enum followed by an empty block</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>